<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js类之继承</title>
</head>
<body>
  <ul>
    <li>使用class定义的类只需要通过extends和super就可以完成继承关系</li>
    <li>使用function定义的类需要通过call或apply完成属性继承, 且需要重新修改原型和回填constructor才能完成继承关系</li>
  </ul>
  <script>
    console.log('****************用class定义类****************')
    {
      class User {
        constructor(name, age) {
          this.name = name
          this.age = age
        }
        show() {
          console.log(`${this.name} is ${this.age} years old.`)
        }
      }
      class Admin extends User {
        constructor(name, age) {
          super(name, age);
        }
      }
      console.dir(Admin)                       // class Admin
      const admin = new Admin('Miracle', 35)
      console.log(admin)                       // Admin {name: "Miracle", age: 35}
      admin.show()                             // Miracle is 35 years old.
    }
    console.log('****************用function定义类****************')
    {
      function User(name, age) {
        this.name = name
        this.age = age
      }
      User.prototype.show = function() {
        console.log(`${this.name} is ${this.age} years old.`)
      }
      function Admin(name, age) {
        User.call(this, name, age)
      }
      Admin.prototype = Object.create(User.prototype)
      Object.defineProperty(Admin.prototype, 'constructor', {
        value: Admin,
        enumerable: false
      })
      console.dir(Admin)                       // ƒ Admin(name, age)
      const admin = new Admin('Miracle', 35)
      console.log(admin)                       // Admin {name: "Miracle", age: 35}
      admin.show()                             // Miracle is 35 years old.
    }
  </script>
</body>
</html>